package com.china08.yxyapi.api.manage.tec.teaching.continuedu.teatrainlesson;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.continuedu.YxyContinueduLesson;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.repository.db.continuedu.YxyContinueduLessonRepository;

/**
 * 
 * @annotation: 继续教育-师训课程-列表
 * @author: yaoguanglin
 */
@CrossOrigin
@RestController
public class YxyTeaTrainLessonSchController extends YxyApiBaseController {

    @Autowired
    private YxyContinueduLessonRepository yxyContinueduLessonRepository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/continuedu/teatrainlesson/sch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        Pageable pageable = reqModel.getPageable(Direction.DESC, reqModel.sort);
        Page<YxyContinueduLesson> page = null;
        // 获取学校所在省的ID
        String areaIdProvince = getProfile(request, response).getSchoolAreaIdProvince();

        if (StringUtils.isBlank(reqModel.keyword)) {
            page = yxyContinueduLessonRepository.findByStatusAndIsVisibleAndAreaCodeProvince(1, 1, areaIdProvince,
                    pageable);
        } else {
            page = yxyContinueduLessonRepository.findByStatusAndIsVisibleAndAreaCodeProvinceAndNameContaining(1, 1,
                    areaIdProvince, reqModel.keyword, pageable);
        }
        List<RespModel> list = new ArrayList<YxyTeaTrainLessonSchController.RespModel>();
        if (page != null && page.getContent().size() > 0) {
            for (YxyContinueduLesson yxyContinueduLesson : page) {
                RespModel model = new RespModel();
                model.count = yxyContinueduLesson.getCount();
                model.cover = yxyContinueduLesson.getCover();
                model.duration = yxyContinueduLesson.getDuration();
                model.id = yxyContinueduLesson.getId();
                model.name = yxyContinueduLesson.getName();
                model.pv = yxyContinueduLesson.getPv();
                model.summary = yxyContinueduLesson.getSummary();
                model.vv = yxyContinueduLesson.getVv();
                list.add(model);
            }
        }

        Page<RespModel> pageRes = new PageImpl<RespModel>(list, pageable, page.getTotalElements());

        return pageRes;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        // 排序规则
        public String sort;
        // 搜索
        public String keyword;

    }

    protected static class RespModel {

        public String id;// 课程ID

        public String name;// 课程名称

        public String summary;// 简介

        public String cover;// 封面

        public Integer count;// 总课时

        public Integer duration;// 总时长

        public Integer pv;// 观看次数

        public Integer vv;// 观看人数

    }
}
